<template>
    <fieldset>
        <legend>货物展示</legend>
        <h1>货物库存</h1>
        <ul>
            <li v-for="item in datas" :key="item.id">
                产品名称{{item.title}}
                ----
                产品价格{{item.price}}
                ----
                产品数量

                {{item.inventory}}

                <button @click="add(item)" :disabled="item.inventory===0">点击加入购物车</button>
            </li>
            
        </ul>
        
    </fieldset>
</template>
<script setup>
import {userUse} from '../../store/uesr'
import {useGoods} from '../../store/goods'
import {storeToRefs} from 'pinia'
let usergoods = userUse()
let good = useGoods()
let {datas} = storeToRefs(usergoods)
let {msg} = storeToRefs(good)
usergoods.fetchgoods()
let add = (item)=>{
    good.$patch((state)=>{
        // state.datas.inventory = state.datas.inventory -1
        let b = state.msg.find(a=>a.id===item.id)
        if(!b){
            let obj = {
                title:item.title,
                price:item.price,
                count:1,
                id:item.id,
                num:item.inventory
            }
            state.msg.unshift(obj)
        }else{
            // this.count++
            b.count++
        }
    })
    item.inventory--
}
</script>